//ctx.request.url
const Koa = require('koa');
const bodyparser = require('koa-bodyparser');
const fs = require('fs');
const app = new Koa();
app.use(bodyparser());

function render(page) {
    return new Promise((resolve, reject) => {
        let pageUrl = `./page/${page}`;
        fs.readFile(pageUrl,'utf-8', (err, data) => {
            if(err) {
                return reject(err);
            }else{
                resolve(data);
            }
        })    
    })
}

async function route(url) {
    let page = '404.html';
    switch(url) {
        case '/':
            page = 'index.html';
            break;
        case '/index':
            page = 'index.html';
            break;
        case '/todo':
            page = 'todo.html';
            break;
        case '/404':
            page = '404.html';
            break;
        default:
            break;
    }
    let html = await render(page);
    return html;
}

app.use(async (ctx) => {
    let url = ctx.request.url;
    let html = await route(url);
    ctx.body = html;
})

app.listen(5000);